import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

// 勋章详情界面 - 显示所有已获得和未获得的勋章，按星星数分类
Dialog {
    id: medalDetailDialog
    title: "🏅 我的勋章"
    anchors.centerIn: parent
    width: 700
    height: 600
    modal: true
    
    background: Rectangle {
        color: "#F5F5F5"
        radius: 15
        border.color: "#FFB300"
        border.width: 2
    }
    
    // 获取所有技能树中的关卡节点
    property var allLevelSkills: {
        var skills = []
        var trees = skillModel.getSkillTrees()
        var originalTreeId = skillModel.currentTreeId
        try {
            for (var i = 0; i < trees.length; i++) {
                var treeId = trees[i].id
                skillModel.currentTreeId = treeId
                // 等待模型更新
                var count = skillModel.count
                for (var j = 0; j < count; j++) {
                    var skill = skillModel.get(j)
                    if (skill && skill.isLevel) {
                        var skillObj = {
                            treeId: treeId,
                            skillId: skill.id,
                            name: skill.name || "未命名关卡",
                            icon: skill.medalIcon || "🏅",
                            starRating: skill.starRating || 0,
                            maxStarRating: skill.maxStarRating || 3,
                            completed: skill.completed || false,
                            treeName: trees[i].name || "未命名技能树"
                        }
                        skills.push(skillObj)
                    }
                }
            }
        } finally {
            // 恢复原始树ID
            skillModel.currentTreeId = originalTreeId
        }
        return skills
    }
    
    // 按星星数分类
    property var medalsByStars: {
        var grouped = {}
        // 先处理已获得的勋章
        for (var i = 0; i < playerModel.medals.length; i++) {
            var medal = playerModel.medals[i]
            if (!medal) continue
            // 从allLevelSkills中找到对应的技能信息
            var skillInfo = null
            for (var j = 0; j < allLevelSkills.length; j++) {
                if (allLevelSkills[j] && 
                    allLevelSkills[j].treeId === medal.treeId && 
                    allLevelSkills[j].skillId === medal.skillId) {
                    skillInfo = allLevelSkills[j]
                    break
                }
            }
            var stars = skillInfo ? skillInfo.starRating : 0
            if (!grouped[stars]) {
                grouped[stars] = {earned: [], unearned: []}
            }
            grouped[stars].earned.push({
                treeId: medal.treeId,
                skillId: medal.skillId,
                name: medal.name || "神秘勋章",
                icon: medal.icon || "🏅",
                starRating: stars,
                maxStarRating: skillInfo ? skillInfo.maxStarRating : 3,
                treeName: skillInfo ? skillInfo.treeName : ""
            })
        }
        // 再处理未获得的勋章
        for (var k = 0; k < allLevelSkills.length; k++) {
            var skill = allLevelSkills[k]
            if (!skill) continue
            var hasMedal = false
            for (var m = 0; m < playerModel.medals.length; m++) {
                if (playerModel.medals[m] && 
                    playerModel.medals[m].treeId === skill.treeId && 
                    playerModel.medals[m].skillId === skill.skillId) {
                    hasMedal = true
                    break
                }
            }
            if (!hasMedal) {
                var skillStars = skill.starRating || 0
                if (!grouped[skillStars]) {
                    grouped[skillStars] = {earned: [], unearned: []}
                }
                grouped[skillStars].unearned.push({
                    treeId: skill.treeId,
                    skillId: skill.skillId,
                    name: skill.name || "未命名关卡",
                    icon: skill.icon || "🏅",
                    starRating: skillStars,
                    maxStarRating: skill.maxStarRating || 3,
                    treeName: skill.treeName || "未命名技能树"
                })
            }
        }
        return grouped
    }
    
    Column {
        anchors.fill: parent
        anchors.margins: 15
        spacing: 15
        
        // 标题
        Text {
            text: "🏅 我的勋章"
            font.pixelSize: 20
            font.bold: true
            color: "#FF6B9D"
            anchors.horizontalCenter: parent.horizontalCenter
        }
        
        // 滚动区域
        ScrollView {
            width: parent.width
            height: parent.height - 80
            clip: true
            
            Column {
                width: parent.width
                spacing: 20
                
                // 按星星数分类显示
                Repeater {
                    model: {
                        var keys = []
                        for (var key in medalsByStars) {
                            if (medalsByStars.hasOwnProperty(key)) {
                                keys.push(parseInt(key))
                            }
                        }
                        keys.sort(function(a, b) { return b - a })  // 从高到低排序
                        return keys
                    }
                    
                    Column {
                        width: parent.width
                        spacing: 10
                        
                        // 星星数标题
                        Text {
                            text: "⭐".repeat(modelData) + " (" + modelData + "星)"
                            font.pixelSize: 16
                            font.bold: true
                            color: "#FF6B9D"
                        }
                        
                        // 已获得的勋章
                        Column {
                            width: parent.width
                            spacing: 5
                            visible: medalsByStars[modelData].earned.length > 0
                            
                            Text {
                                text: "已获得 (" + medalsByStars[modelData].earned.length + ")"
                                font.pixelSize: 12
                                color: "#666666"
                            }
                            
                            Flow {
                                width: parent.width
                                spacing: 10
                                
                                Repeater {
                                    model: medalsByStars[modelData].earned
                                    
                                    Rectangle {
                                        width: 60
                                        height: 80
                                        radius: 8
                                        color: "#FFFFFF"
                                        border.color: "#FFB300"
                                        border.width: 2
                                        
                                        Column {
                                            anchors.centerIn: parent
                                            spacing: 4
                                            
                                            Rectangle {
                                                width: 40
                                                height: 40
                                                radius: 20
                                                color: "#FFFFFF"
                                                border.color: "#FFB300"
                                                border.width: 1.5
                                                anchors.horizontalCenter: parent.horizontalCenter
                                                
                                                property var medal: modelData
                                                property bool isImage: typeof medal.icon === "string" &&
                                                                       (medal.icon.startsWith("qrc:") || medal.icon.startsWith("file:"))
                                                
                                                Text {
                                                    anchors.centerIn: parent
                                                    text: parent.isImage ? "" : (parent.medal.icon || "🏅")
                                                    font.pixelSize: 24
                                                    visible: !parent.isImage
                                                }
                                                
                                                Image {
                                                    anchors.fill: parent
                                                    anchors.margins: 4
                                                    source: parent.isImage ? parent.medal.icon : ""
                                                    visible: parent.isImage
                                                    fillMode: Image.PreserveAspectFit
                                                }
                                            }
                                            
                                            Text {
                                                text: modelData.name || "神秘勋章"
                                                font.pixelSize: 10
                                                color: "#333333"
                                                width: 55
                                                elide: Text.ElideRight
                                                horizontalAlignment: Text.AlignHCenter
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }
                                        }
                                        
                                        MouseArea {
                                            anchors.fill: parent
                                            hoverEnabled: true
                                            cursorShape: Qt.PointingHandCursor
                                            
                                            ToolTip.visible: containsMouse
                                            ToolTip.text: (modelData.treeName ? modelData.treeName + " - " : "") + 
                                                          (modelData.name || "神秘勋章")
                                        }
                                    }
                                }
                            }
                        }
                        
                        // 未获得的勋章
                        Column {
                            width: parent.width
                            spacing: 5
                            visible: medalsByStars[modelData].unearned.length > 0
                            
                            Text {
                                text: "未获得 (" + medalsByStars[modelData].unearned.length + ")"
                                font.pixelSize: 12
                                color: "#999999"
                            }
                            
                            Flow {
                                width: parent.width
                                spacing: 10
                                
                                Repeater {
                                    model: medalsByStars[modelData].unearned
                                    
                                    Rectangle {
                                        width: 60
                                        height: 80
                                        radius: 8
                                        color: "#E0E0E0"
                                        border.color: "#CCCCCC"
                                        border.width: 1
                                        opacity: 0.5
                                        
                                        Column {
                                            anchors.centerIn: parent
                                            spacing: 4
                                            
                                            Rectangle {
                                                width: 40
                                                height: 40
                                                radius: 20
                                                color: "#E0E0E0"
                                                border.color: "#CCCCCC"
                                                border.width: 1
                                                anchors.horizontalCenter: parent.horizontalCenter
                                                
                                                property var medal: modelData
                                                property bool isImage: typeof medal.icon === "string" &&
                                                                       (medal.icon.startsWith("qrc:") || medal.icon.startsWith("file:"))
                                                
                                                Text {
                                                    anchors.centerIn: parent
                                                    text: parent.isImage ? "" : (parent.medal.icon || "🏅")
                                                    font.pixelSize: 24
                                                    visible: !parent.isImage
                                                    opacity: 0.5
                                                }
                                                
                                                Image {
                                                    anchors.fill: parent
                                                    anchors.margins: 4
                                                    source: parent.isImage ? parent.medal.icon : ""
                                                    visible: parent.isImage
                                                    fillMode: Image.PreserveAspectFit
                                                    opacity: 0.5
                                                }
                                            }
                                            
                                            Text {
                                                text: modelData.name || "神秘勋章"
                                                font.pixelSize: 10
                                                color: "#999999"
                                                width: 55
                                                elide: Text.ElideRight
                                                horizontalAlignment: Text.AlignHCenter
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }
                                        }
                                        
                                        MouseArea {
                                            anchors.fill: parent
                                            hoverEnabled: true
                                            cursorShape: Qt.PointingHandCursor
                                            
                                            ToolTip.visible: containsMouse
                                            ToolTip.text: (modelData.treeName ? modelData.treeName + " - " : "") + 
                                                          (modelData.name || "神秘勋章") + " (未获得)"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                
                // 如果没有勋章
                Text {
                    text: "还没有勋章，挑战关卡加油！"
                    font.pixelSize: 14
                    color: "#999999"
                    anchors.horizontalCenter: parent.horizontalCenter
                    visible: playerModel.medals.length === 0 && allLevelSkills.length === 0
                }
            }
        }
        
        // 关闭按钮
        Button {
            text: "关闭"
            anchors.horizontalCenter: parent.horizontalCenter
            font.pixelSize: 15
            onClicked: medalDetailDialog.close()
        }
    }
}

